function R = rank_nonan(A)

% If the matrix A contains no NaN, Inf or -Inf elements, 
% rank_nonan(A) is simply the rank of A, i.e. R = rank(A).
% If at leat one element of A is NaN, Inf or -Inf, R = 0.

% G. Teza, 2005

if (sum(isnan(A(:))) > 0) || (sum(isinf(A(:))) > 0)
    R = 0;
else
    R = rank(A);
end